package gui.bf3;

import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

import file.SettingsDefinition;
import file.ValueObject;
import gui.VideoSettingsPanel;

public class BF3ComboBox extends JComboBox implements BF3Setting {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1056623011203409767L;
	protected SettingsDefinition settingsDefinition;
	protected SettingsDefinition[] settingsDefinitions;

	public BF3ComboBox(SettingsDefinition settingsDefinition) {
		super();
		this.settingsDefinition = settingsDefinition;
	}

	public BF3ComboBox(SettingsDefinition settingsDefinition, ComboBoxModel comboBoxModel) {
		super(comboBoxModel);
		this.settingsDefinition = settingsDefinition;
	}

	public BF3ComboBox(SettingsDefinition settingsDefinition, Object[] object) {
		super(object);
		this.settingsDefinition = settingsDefinition;
	}

	public BF3ComboBox(SettingsDefinition settingsDefinition, Vector<?> vector) {
		super(vector);
		this.settingsDefinition = settingsDefinition;
	}

	public BF3ComboBox(SettingsDefinition[] settingsDefinitions, Object[] object) {
		super(object);
		this.settingsDefinitions = settingsDefinitions;
	}

	@Override
	public void updateValue() {

		int selectedIndex = 0;

		String actionCommand = this.getActionCommand();
		if (actionCommand.equals("resolution")) {

			String[] resolutionValues = new String[3];

			for (int i = 0; i < resolutionValues.length; i++) {
				ValueObject[] valuesObjects = settingsDefinitions[i].getValueObjects();
				if (valuesObjects != null) {
					resolutionValues[i] = valuesObjects[1].getValue();
				}
			}
			selectedIndex = VideoSettingsPanel.getIndexOfResolution(resolutionValues);

		} else if (actionCommand.equals("soundSystemSize")) {
			if(settingsDefinition != null){
				ValueObject[] valuesObjects = settingsDefinition.getValueObjects();
				if (valuesObjects != null) {
					String value = valuesObjects[1].getValue().substring(0, 1);
					selectedIndex = Integer.parseInt(value) - 2;
				}
			}
		}else if(actionCommand.equals("verticalLook")){
			ValueObject[] valuesObjects1 = settingsDefinitions[0].getValueObjects();
			ValueObject[] valuesObjects2 = settingsDefinitions[1].getValueObjects();
			if(valuesObjects1 != null && valuesObjects2 != null){
				String firstValue = valuesObjects1[1].getValue();
				String secondValue = valuesObjects2[1].getValue();
				if(firstValue.equals("1") && secondValue.equals("1")){
					selectedIndex = 1;
				}else{
					selectedIndex = 0;
				}
			}
		}else if(actionCommand.equals("shaderQuality")){
			if(settingsDefinition != null){
				ValueObject[] valuesObjects = settingsDefinition.getValueObjects();
				if (valuesObjects != null) {
					selectedIndex = (int) Float.parseFloat(valuesObjects[1].getValue());
				}
			}
		}else if(actionCommand.equals("anisotropicFilter")){
			if(settingsDefinition != null){
				ValueObject[] valuesObjects = settingsDefinition.getValueObjects();
				if (valuesObjects != null) {
					selectedIndex = Integer.parseInt(valuesObjects[1].getValue());
					if(selectedIndex == 1)
						selectedIndex = 0;
					else if (selectedIndex == 2)
						selectedIndex = 1;
					else if (selectedIndex == 4)
						selectedIndex = 2;
					else if (selectedIndex == 8)
						selectedIndex = 3;
					else if (selectedIndex == 16)
						selectedIndex = 4;
				}
			}
		} else {
			if(settingsDefinition != null){
				ValueObject[] valuesObjects = settingsDefinition.getValueObjects();
				if (valuesObjects != null) {
					selectedIndex = Integer.parseInt(valuesObjects[1].getValue());
				}
			}
		}
		this.setSelectedIndex(selectedIndex);

	}

	@Override
	public void setValue(String value) {
		// mutlisettings
		if (settingsDefinitions != null) {
			String[] values = value.split(",");
			for (int i = 0; i < settingsDefinitions.length; i++) {
				ValueObject[] valueObjects = settingsDefinitions[i].getValueObjects();
				if (valueObjects != null) {
					valueObjects[1].setValue(values[i]);
				}
			}
		// single setting
		} else {
			ValueObject[] valueObjects = settingsDefinition.getValueObjects();
			if (valueObjects != null) {
				if(this.getActionCommand().equals("soundSystemSize")){
					String woofer = valueObjects[1].getValue().substring(1);
					value = value+woofer;
				}
				valueObjects[1].setValue(value);
			}
		}
	}

}
